<?php
/**
 * Statistic Class
 * @author haRd (w3-hack) / mod by toward
 */
class Statistics
{
	public function CountBots()
	{
		$bots = array();
		//if(Database::execute("UPDATE bots SET status = 0"))
		if(Database::query("UPDATE bots SET status = 0"))
		{
			$result = Database::execute("UPDATE bots SET status = 1 WHERE (? - time) < 600", time()); // status = 0 WHERE (".time()." - bots.time) > 600");
			if($result)
			{
				$result = Database::query("SELECT * FROM bots WHERE status = 1");
				$bots['online'] = $result->num_rows;
				if(empty($result))
					$bots['online'] = 0;
			}
			else
				$bots['online'] = 0;
		}
		else
			$bots['online'] = 0;
		
		$result = Database::query("SELECT * FROM bots");
		if($result)
			$bots['all'] = $result->num_rows;
		if(empty($result))
			$bots['all'] = 0;
	
		$result = Database::query("SELECT * FROM bots WHERE (? - time) > 604800", time()); // select bots whose last connection is older than a week
		$bots['dead'] = $result->num_rows;
		if(empty($result))
			$bots['dead'] = 0;
			
		$bots['offline'] = $bots['all'] - $bots['online'];
		
		return $bots;
	}
	
	public function FetchOS()
	{
		$result = Database::getObjects("SELECT os FROM bots");
		if($result)
		{
			$os = array();
			foreach ($result as $row)
			{
				if(!in_array($row['os'], $os) )
					$os[] = $row['os'];
			}
			return $os;
		}
		else
			return;
		
	}
	
	public function CountOS($os)
	{
		$count = array();
		for($i=0; $i<sizeof($os); $i++)
		{
			$query = "SELECT * FROM bots WHERE os = ?";
			$execute = Database::query($query, $os[$i]);
			$count[$i] = $execute->num_rows;
		}

		return $count;
	}
	
	public function FetchCountries()
	{
		$result = Database::getObjects("SELECT country FROM bots");
		if($result)
		{
			$countries = array();
			foreach ($result as $row)
			{
				if(!in_array($row['country'], $countries) )
					$countries[] = $row['country'];
			}
			return $countries;
		}
		else
			return;
		
	}
	
	public function CountCountries($countries)
	{
		$count = array();
		for($i=0; $i<sizeof($countries); $i++)
		{
			$query = "SELECT * FROM bots WHERE country = ?";
			$execute = Database::query($query, $countries[$i]);
			if ($execute)
				$count[$i] = $execute->num_rows;
			else 
				$count[$i] = 0;
		}
		
		return $count;
	}
}
?>